package ru.startandroid.firstproj.firstproj;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
    }

    class MyView extends View
    {
        Paint p;
        Rect rect;
        RectF rectf;
        Rect rectClip;
        StringBuilder sb;
        maze m;
        object player;
        boolean maze_painted;
        float evX_start = 0, evY_start = 0, evX_end = 0, evY_end = 0;
        byte[] lurd = new byte[4]; // [0] - -x, [1] - -y, [2] - +x, [3] - +y;


        public MyView(Context context)
        {
            super(context);
            p = new Paint();
            rect = new Rect();
            rectf = new RectF();
            sb = new StringBuilder();
            m = new maze();
            player = new object();
            maze_painted = false;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event)
        {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    evX_start = event.getX();
                    evY_start = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    evY_end = event.getY();
                    evX_end = event.getX();
                    functions.move_player(m, player, evX_start, evY_start, evX_end, evY_end, lurd);
                    //functions.draw_player(canvas, p, player);
                    invalidate();
                    break;
            }
            return true;
        }

        @Override
        protected void onDraw(Canvas canvas)
        {
            canvas.drawARGB(200, 0, 0, 128);
            if ( !maze_painted ){
                functions.frame_maze(canvas, m);
                //player.place(m, m.get().x >> 2, m.get().y >> 2, m.get().x >> 2, m.get().y >> 2);
                player.x = 1;
                player.y = 1;
                maze_painted = true;
            }
            functions.draw_maze(canvas, p, rect, m, player, rectClip, lurd);
            functions.draw_player(canvas, m, p, player, rectClip);

            /*p.setColor(Color.WHITE);
            String s = "generation_before last: "+m.time_gen_before + " ms;";
            p.setStrokeWidth(2);
            p.setTextSize(28);
            canvas.drawText(s, 20, 50, p);

            s = "generation_after last: "+m.time_gen_after + " ms;";
            canvas.drawText(s, 20, 100, p);*/
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
